package com.gc.ykqx.service.presenter;


import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.gc.ykqx.service.entity.WeaResponse;
import com.gc.ykqx.service.manager.DataManager;
import com.gc.ykqx.service.view.SwWeaView;
import com.gc.ykqx.service.view.View;
import com.gc.ykqx.service.util.Constant;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by wsd on 2017/9/30.
 */

public class SwWeaPresenter implements Presenter {
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private Context mContext;
    private SwWeaView mSwWeaView;
    private WeaResponse mWeaResponse;

    public SwWeaPresenter(Context context) {
        this.mContext = context;
    }

    @Override
    public void onCreate() {
        manager = new DataManager(mContext);
        mCompositeSubscription = new CompositeSubscription();

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }

    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(View view) {
        mSwWeaView = (SwWeaView) view;

    }

    @Override
    public void attachIncomingIntent(Intent intent) {

    }

    public void getWeather(String selectId, String staIds, String time, String siteType, String beginTime, String endTime, String diyType) {
        switch (selectId) {
            case "n20":
            case "n30":
                getWeaSwHor(staIds, time, Constant.Element.PRE_SW_1H);
                break;
            case "n21":
            case "n31":
                getWeaSwHor(staIds, time, null);
                break;
            case "n22":
            case "n32":
                getWeaSwHor(staIds, time, null);
                break;
            case "n23":
            case "n33":
                getWeaSwHor(staIds, time, null);
                break;
            case "n24":
            case "n34":
                getWeaSwHor(staIds, time, null);
                break;
            case "n25":
            case "n35":
                getWeaSwHor(staIds, time, null);
                break;
            case "n26":
            case "n36":
                getWeaSwHor(staIds, time, null);
                break;
            case "n27":
            case "n37":
                getWeaSwHor(staIds, time, null);
                break;
            case "n28":
            case "n38":
                getWeaSwHor(staIds, time, null);
                break;
            case "n29":
            case "n39":
                getWeaSwHor(staIds, time, null);
                break;
        }
    }

    private void getWeaSwHor(String staIds, String time, String element) {
        mCompositeSubscription.add(manager.getWeaSwHor(staIds, time, element)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeaResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mWeaResponse != null) {
                            //数据解析
                            mSwWeaView.onSuccess(mWeaResponse);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(WeaResponse weaResponse) {
                        mWeaResponse = weaResponse;
                    }
                }));

    }

}
